﻿
@{
    ViewData["Title"] = "Index";
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../layui/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../layui/css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
    <div class="layuimini-container">

        <div class="layuimini-main">
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">菜单名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="username" id="edtSearch" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn" id="btnSearch"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                           
                        </div>
                        </form>
                </div>
            </fieldset>

            <div>
                <div class="layui-btn-group" style="margin-top:10px">
                    <button class="layui-btn layui-btn-primary layui-border-green" id="btn-expand">全部展开</button>
                </div>
                <div class="layui-btn-group" style="margin-top:10px">
                    <button class="layui-btn layui-btn-primary layui-border-orange" id="btn-fold">全部折叠</button>
                </div>
                <div class="layui-btn-group" style="margin-top:10px">
                    <button class="layui-btn layui-btn-primary layui-border-blue" id="btn_add">添加菜单</button>
                </div>
                <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
            </div>

        </div>
    </div>
    <!-- 操作列 -->
    <script type="text/html" id="auth-state">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script src="../layui/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="../layui/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script>
        layui.use(['table', 'treetable'], function () {
            var $ = layui.jquery;
            var table = layui.table;
            var treetable = layui.treetable;

            // 渲染表格
            layer.load(2);
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'parentId',
                elem: '#munu-table',
                url: '/MenuInfo/getMenuTreetab',
                page: false,
                id: 'demoId', //定义索引
                cols: [[
                    { type: 'numbers' },
                    { field: 'title', minWidth: 200, title: '权限名称' },
                    { field: 'icon', title: '图标' },
                    { field: 'href', title: '菜单url' },
                    { field: 'sort', width: 80, align: 'center', title: '排序号' },
                    {
                        field: 'isMenu', width: 80, align: 'center', templet: function (d) {
                            if (d.isMenu == 1) {
                                return '<span class="layui-badge layui-bg-gray">按钮</span>';
                            }
                            if (d.href == null) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }
                        }, title: '类型'
                    },
                    { templet: '#auth-state', width: 120, align: 'center', title: '操作' }
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });

            $('#btn-expand').click(function () {
                treetable.expandAll('#munu-table');
            });

            $('#btn-fold').click(function () {
                treetable.foldAll('#munu-table');
            });


            // 搜索按钮点击事件 根据输入内容突出显示符合条件的文本
            $('#btnSearch').click(function () {
                var keyword = $('#edtSearch').val();
                var $tds = $('#munu-table').next('.treeTable').find('.layui-table-body tbody tr td');
                if (!keyword) {
                    $tds.css('background-color', 'transparent');
                    layer.msg("请输入关键字", { icon: 5 });
                    return;
                }
                var searchCount = 0;
                $tds.each(function () {
                    $(this).css('background-color', 'transparent');
                    if ($(this).text().indexOf(keyword) >= 0) {
                        $(this).css('background-color', 'rgba(250,230,160,0.5)');
                        if (searchCount == 0) {
                            $('body,html').stop(true);//火狐 ie不支持body,谷歌支持的是body，所以为了兼容写body和html   stop()方法停止当前正在运行的动画
                            $('body,html').animate({ scrollTop: $(this).offset().top - 150 }, 500);
                        }
                        searchCount++;
                    }
                });
                if (searchCount == 0) {
                    layer.msg("没有匹配结果", { icon: 5 });
                } else {
                    treetable.expandAll('#authTable');
                }
            });

            //添加
            $('#btn_add').click(function () {
                var index = layer.open({
                    title: '添加部门',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/MenuInfo/AddMenuInfoView',
                    end: function () {
                        window.location.reload()
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });

            });

            //监听工具条
            table.on('tool(munu-table)', function (obj) {
                var data = obj.data;
                var layEvent = obj.event;
                if (layEvent === 'del') {
                    layer.confirm('真的要删除菜单："' + data.title + '" 吗?', { icon: 3 }, function (index) {
                        //发送删除请求
                        $.ajax({
                            url: "/MenuInfo/DalateMenuInfo",
                            type: "Delete",
                            data: { menuInfoid: data.id },
                            success: function (res) {
                                layer.msg(res.msg, { icon: 1 })
                                if (res.data) {
                                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                    layer.close(index);
                                }
                            }
                        })
                    });
                } else if (layEvent === 'edit') {

                    //打开修改页面
                    var index = layer.open({
                        title: '编辑菜单',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/MenuInfo/UpdataMenuInfoView?id=' + data.id,
                        end: function () {
                            window.location.reload()
                        }
                    });

                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                }
            });


        });


    </script>
</body>
</html>